import { reqGetDetailList, reqAddOrUpdateShopCart } from "../../api/index";
// 封装游客身份模块uuid ----> 生成一个随机字符串(生成一次不能再变了)
import {getUUID} from '../../utils/uuid-token'
const state = {
  detailList: {},
  // 顾客临时身份
  uuid_token:getUUID()
};
const actions = {
  // 获取商品详情
  async getDetailList({ commit }, skuId) {
    let result = await reqGetDetailList(skuId);
    if (result.status === 200) {
      commit("GETDETAILLIST", result.data.data);
    }
  },
  // 将产品添加到购物车
  async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
    let result = await reqAddOrUpdateShopCart(skuId, skuNum);
    // 代表服务器加入购物车成功
    if (result.data.code === 200) {
      return "ok";
      // 代表服务器加入购物车失败
    } else {
      return Promise.reject(new Error("faile"));
    }
  },
};
const mutations = {
  GETDETAILLIST(state, detailList) {
    state.detailList = detailList;
  },
};
const getters = {
  // 取出导航路径信息
  categoryView(state) {
    return state.detailList.categoryView || {};
  },
  skuInfo(state) {
    return state.detailList.skuInfo || {};
  },
  spuSaleAttrList(state) {
    return state.detailList.spuSaleAttrList || [];
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
